﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace EnvironmentManagerSystem
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.textfield.Focus();
            }
        }

        //登陆信息
        protected void ok_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (textfield.Text.Equals("") || textfield2.Text.Equals("")) Jscript.Alert("用户名或密码不能为空！", this);
                else
                {
                    DataTable dt = EnvironmentManagerSystem.BLL.User.login(textfield.Text.Trim(), textfield2.Text.Trim());
                    if (dt.Rows.Count > 0)
                    {
                        Session.Timeout = 60;
                        Session["ID"] = dt.Rows[0]["ID"].ToString().ToString();
                        Session["roleid"] = dt.Rows[0]["roleid"].ToString().ToString();
                        Session["UserName"] = dt.Rows[0]["UserName"].ToString().ToString();
                        Session["UID"] = dt.Rows[0]["LoginName"].ToString().ToString();
                        Session["userid"] = dt.Rows[0]["userid"].ToString().ToString();
                        Session["evaluationunitID"] = dt.Rows[0]["evaluationunitID"].ToString().ToString();
                        foreach (string RoleId in dt.Rows[0]["roleid"].ToString().Split(','))
                        {
                            #region 用户权限
                            if (RoleId == "管理员")
                            {
                                Session["Admin"] = true;
                                Session["xmfzr"] = false;

                                Session["gly"] = false;
                                break;
                            }
                            else if (RoleId == "部门负责人")
                            {
                                Session["xmfzr"] = true;
                                Session["Admin"] = false;

                                Session["gly"] = false;
                                break;
                            }
                            else
                            {
                                Session["xmfzr"] = false;
                                Session["Admin"] = false;

                                Session["gly"] = false;
                                if (RoleId == "建设单位")
                                    Response.Redirect("Default.aspx");
                                if (RoleId == "环评单位")
                                    Response.Redirect("EvaluationUnitManager/index.aspx");
                                if (RoleId == "财务")
                                    Response.Redirect("Finance/Index.aspx");
                                if (RoleId == "环境局")
                                    Response.Redirect("HuanTong/HTUserIndex.aspx");
                                if (RoleId == "环境局管理员")
                                {
                                    Session["ZhiNeng"] = "";
                                    string a = Session["ZhiNeng"].ToString();
                                    Session["gly"] = true;
                                    Response.Redirect("HuanTong/HTUserIndex.aspx");
                                }
                            }
                            #endregion
                        }
                        Response.Redirect("index.aspx");
                    }
                    else
                    {
                        Jscript.Alert("用户名或密码不正确！", this);
                        this.textfield.Text = "";
                        this.textfield2.Text = "";
                        this.textfield.Focus();
                    }
                }
            }
            catch { }
        }
    }
}
